package gota.processingResources;

import java.io.File;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import gate.Factory;
import gate.FeatureMap;
import gate.creole.SerialAnalyserController;

import org.w3c.dom.Node;

public class NETransducerResource implements IProcessingResource {

	@Override
	public String getResourceName() {
		return "NETransducer";
	}

	@Override
	public void init(SerialAnalyserController controller, Node node)
			throws Exception {
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		org.w3c.dom.Node grammarUrlNode = (org.w3c.dom.Node) xpath.evaluate(
				"//GrammarUrl", node, XPathConstants.NODE);
		String grammarUrl = grammarUrlNode.getTextContent();

		FeatureMap params = Factory.newFeatureMap();
		params.put(
				gate.creole.ANNIETransducer.TRANSD_GRAMMAR_URL_PARAMETER_NAME,
				new File(grammarUrl).toURI().toURL());
		params.put(gate.creole.ANNIETransducer.TRANSD_ENCODING_PARAMETER_NAME,
				"UTF-8");

		FeatureMap features = Factory.newFeatureMap();
		gate.creole.ANNIETransducer annieTransducer = (gate.creole.ANNIETransducer) Factory
				.createResource("gate.creole.ANNIETransducer", params, features);

		controller.add(annieTransducer);
	}

}
